package spring.boot.wx.mini.controller;

import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import spring.boot.wx.mini.common.Results;
import spring.boot.wx.mini.service.WxMiniUserService;
import spring.boot.wx.mini.vo.UserAuthVO;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(tags = "权限接口")
public class LoginController {

    @Autowired
    private WxMiniUserService wxMiniUserService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "登录时获取的 code", required = true, dataType = "String", paramType = "path", example = "a1s2d3")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "接口返回成功"),
            @ApiResponse(code = 100, message = "参数传递错误，检查参数是否合法"),
            @ApiResponse(code = 200, message = "接口返回未知错误，请联系开发人员调试"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "登录接口", notes = "访问此接口，进行微信小程序简单登录")
    @PostMapping("/wx/mini/login/{code}")
    public Results login(@PathVariable String code) {
        return wxMiniUserService.login(code);
    }


    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "接口返回成功"),
            @ApiResponse(code = 100, message = "参数传递错误，检查参数是否合法"),
            @ApiResponse(code = 405, message = "token错误"),
            @ApiResponse(code = 401, message = "登录过期或者未登录"),
            @ApiResponse(code = 200, message = "接口返回未知错误，请联系开发人员调试"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "授权接口", notes = "访问此接口，进行微信小程序授权")
    @PostMapping("/wx/mini/auth")
    public Results auth(@RequestBody UserAuthVO userAuthVO, HttpServletRequest request) {
        log.info("访问授权接口--------------------------------------------------");
        return wxMiniUserService.auth(userAuthVO, request);
    }
}
